﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Data;

public partial class EarlyWarning_WarningSetup : BasePage
{
    public EarlyWarning_WarningSetup()
    { }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnEnter.Attributes.Add("onclick", "return CheckInput()");
        this.dropCity.Attributes.Add("onchange", "selectChangeHospital()");
        this.dropType.Attributes.Add("onchange", "selectChangeDrugs()");
        if (!this.IsPostBack)
        {
            BindArea();
            BindSort();
            dataBind();

        }
        txtHospital.Attributes.Add("readonly", "readonly");
        txtDrugsName.Attributes.Add("readonly", "readonly");
    }

    private void BindArea()
    {
        IList<Area> lstArea = new List<Area>();
        IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
        int outcount;
        lstArea = ia.GetSonAreas(CurrentAccount.GetUser().Org.Area.AreaID, 1, 0, out outcount);
        foreach (Area area in lstArea)
        {
            ListItem ddlitem = new ListItem();
            ddlitem.Text = area.AreaName;
            ddlitem.Value = area.AreaID;
            dropCity.Items.Add(ddlitem);
        }

    }

    private void BindSort()
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            GroupField = GroupByField.SortID,
            SortField = "SortID"
        };
        System.Data.DataTable dt = ie.Execute();

        this.dropType.DataTextField = "SortName";
        this.dropType.DataValueField = "SortId";
        this.dropType.DataSource = dt;
        this.dropType.DataBind();

        ListItem item = new ListItem();
        item.Text = "全部";
        item.Value = "";
        dropType.Items.Insert(0, item);
    }

    private void dataBind()
    {
        int t = 0;
        HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();
        System.Data.DataTable dt = pre.GetDrugPreWarning(null, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row["HospitalID"].ToString() != "所有医院"&&row["HospitalName"]==DBNull.Value)
                {
                    row["HospitalName"] = "多家医院";
                }
            }
        }
        AspNetPager1.RecordCount = t;
        this.gridInfo.DataSource = dt;
        this.gridInfo.DataBind();
    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        string _AreaID = this.dropCity.SelectedItem.Value;   //区域编号
        string _Hospital = HZ.Fun.CheckString(this.txtHospital.Text.Trim()); //医院名称
        string _HospitalId = HZ.Fun.CheckString(this.hddHospitalId.Value);//医院编号

        string _DrugsType = this.dropType.SelectedItem.Value == "" ? "所有类型" : this.dropType.SelectedItem.Value;//药品类型
        string _DrugsName = HZ.Fun.CheckString(this.txtDrugsName.Text); //药品名称
        string _DrugsId = HZ.Fun.CheckString(this.hddGoodsId.Value);    //药品编号

        string _Num = HZ.Fun.CheckString(this.txtNum.Text);
        string _Amount = HZ.Fun.CheckString(this.txtAmount.Text);
        string _Level = this.dropLevel.SelectedItem.Value;
        string _Range = HZ.Fun.CheckString(this.txtRange.Text);
        string _date = this.dropTime.SelectedItem.Text;

        if (string.IsNullOrEmpty(_Hospital)) { _HospitalId = "所有医院"; }

        if (string.IsNullOrEmpty(_DrugsName)) { _DrugsId = "所有药品"; }

        HZ.Supervise.Model.DrugPreWarning drugwar = new DrugPreWarning();
        HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();
        drugwar.AreaID = _AreaID;
        drugwar.HospitalID = _HospitalId;
        drugwar.SortID = _DrugsType;
        drugwar.ProcureCatalogID = _DrugsId;

        if (!string.IsNullOrEmpty(_Num))
        {
            if (!HZ.Fun.IsInt(_Num))
            {
                HZ.Web.WebUtil.Alert("预警数量有误!");
                return;
            }
            else {
                drugwar.WarningCount = int.Parse(_Num);
                if (drugwar.WarningCount <=0)
                {
                    HZ.Web.WebUtil.Alert("预警数量不能小于零！");
                    return;
                }
            }
        }

        if (!string.IsNullOrEmpty(_Amount))
        {
            if (!HZ.Fun.IsFloat(_Amount))
            {
                HZ.Web.WebUtil.Alert("预警金额有误！");
                return;
            }
            else {
                drugwar.WarningAmount = decimal.Parse(_Amount);
                if (drugwar.WarningAmount <= 0)
                {
                    HZ.Web.WebUtil.Alert("预警金额不能小于零！");
                    return;
                }
            }
        }

        if (!HZ.Fun.IsInt(_Range))
        {
            HZ.Web.WebUtil.Alert("预警范围有误！");
            return;
        }

        drugwar.WarningLevel = int.Parse(this.dropLevel.SelectedItem.Value);

        if (pre.DrugCheckDataSource(drugwar))
        {
            HZ.Web.WebUtil.Alert("该医院已设置预警！");
            return;
        }

        int _time = 0;
        switch (_date)
        {
            case "天":
                _time = int.Parse(_Range);
                break;
            case "周":
                _time = int.Parse(_Range) * 7;
                break;
            case "月":
                _time = int.Parse(_Range) * 30;
                break;
        }

        drugwar.WarningDate = _time;

        if (pre.AddDrugPreWarning(drugwar) > 0)
        {
            HZ.Web.WebUtil.Alert("添加成功！");
            HZ.Web.WebUtil.RedirectByScript("WarningSetup.aspx");
            dataBind();
        }

    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        dataBind();
    }

    protected void gridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label llabLeve = (Label)e.Row.Cells[9].FindControl("labLevel");

            switch (llabLeve.Text)
            {
                case "1":
                    llabLeve.Text = "一级预警";
                    break;
                case "2":
                    llabLeve.Text = "二级预警";
                    break;
                case "3":
                    llabLeve.Text = "三级预警";
                    break;
            }

            string _HospitalId = ((DataRowView)e.Row.DataItem).Row.ItemArray[9].ToString();
            string _AreaId = ((DataRowView)e.Row.DataItem).Row.ItemArray[1].ToString();

            e.Row.Cells[2].Attributes.Add("onclick", "javascript:window.open('showHospital.aspx?HospitalID=" + Server.UrlEncode(_HospitalId) + "&AreaID=" + Server.UrlEncode(_AreaId) + "','showHospital','width=600,height=400,resizable=0,scrollbars=0, status=0, toolbar=0, menubar=0')");
            e.Row.Cells[2].Attributes.Add("onmouseover","this.style.color='red'");
            e.Row.Cells[2].Attributes.Add("onmouseout", "this.style.color=''");
        }
    }



}
